#include "morse.h"

MorseConverter::MorseConverter() {
    // 初始化莫尔斯码映射
    morseMap['A'] = ".-";
    morseMap['B'] = "-...";
    morseMap['C'] = "-.-.";
    morseMap['D'] = "-..";
    morseMap['E'] = ".";
    morseMap['F'] = "..-.";
    morseMap['G'] = "--.";
    morseMap['H'] = "....";
    morseMap['I'] = "..";
    morseMap['J'] = ".---";
    morseMap['K'] = "-.-";
    morseMap['L'] = ".-..";
    morseMap['M'] = "--";
    morseMap['N'] = "-.";
    morseMap['O'] = "---";
    morseMap['P'] = ".--.";
    morseMap['Q'] = "--.-";
    morseMap['R'] = ".-.";
    morseMap['S'] = "...";
    morseMap['T'] = "-";
    morseMap['U'] = "..-";
    morseMap['V'] = "...-";
    morseMap['W'] = ".--";
    morseMap['X'] = "-..-";
    morseMap['Y'] = "-.--";
    morseMap['Z'] = "--..";
    
    morseMap['0'] = "-----";
    morseMap['1'] = ".----";
    morseMap['2'] = "..---";
    morseMap['3'] = "...--";
    morseMap['4'] = "....-";
    morseMap['5'] = ".....";
    morseMap['6'] = "-....";
    morseMap['7'] = "--...";
    morseMap['8'] = "---..";
    morseMap['9'] = "----.";
    
    morseMap['?'] = "..--..";
    morseMap['='] = "-...-";
    morseMap[','] = "--..--";
    morseMap['.'] = "-.-.-.-";
    morseMap['/'] = "-..-.";
}

MorseConverter::~MorseConverter() {
}

std::string MorseConverter::charToMorse(char c) const {
    // 转换为大写
    if (c >= 'a' && c <= 'z') {
        c = c - 32;
    }
    
    // 查找莫尔斯码
    auto it = morseMap.find(c);
    if (it != morseMap.end()) {
        return it->second;
    }
    
    // 如果找不到，返回空字符串
    return "";
}

std::string MorseConverter::stringToMorse(const std::string& str) const {
    std::string result;
    
    for (char c : str) {
        std::string morse = charToMorse(c);
        if (!morse.empty()) {
            if (!result.empty()) {
                result += " ";  // 字符之间用空格分隔
            }
            result += morse;
        }
    }
    
    return result;
}